package com.shop2cn.iapi.agentproducts.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.shop2cn.iapi.agentproducts.parameter.QueryRebateRateBean;
import com.shop2cn.iapi.agentproducts.service.QueryRebateRateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;

/**
 * 查询分销小店商品佣金Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryRebateRate {
	private static QueryRebateRateBean queryrebaterateBean;
	private static QueryRebateRateCall queryrebaterateCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询分销小店商品佣金Test");
	}

	@Before
	public void caseUp() {
		queryrebaterateBean = new QueryRebateRateBean();
		queryrebaterateCall = new QueryRebateRateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryrebaterateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryRebateRate_001() {
		Logger.start(true, "查询分销小店商品佣金，成功");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);

			List<ProductListDto> getProductLists = AddAgentProductCallService.GetProductLists(AgentUserId);
			System.out.println(getProductLists);

			ProductDetailsBean initGetProductDetails = AddAgentProductCallService.initGetProductDetails(userId,
					AgentUserId, productId, true);
			ProductDetailsDto selectProductDetails = AddAgentProductCallService
					.GetProductDetailsCall(initGetProductDetails);

			List<String> catalogs = new ArrayList<>();
			selectProductDetails.getCatalogList().forEach(c -> {
				catalogs.add(c.getCatalogId());
			});

			ProductDetailsBean initGetProductDetails1 = AddAgentProductCallService.initGetProductDetails(userId,
					AgentUserId, productId, true);
			ProductDetailsDto selectProductDetails1 = AddAgentProductCallService
					.GetProductDetailsCall(initGetProductDetails1);

			List<String> catalogs1 = new ArrayList<>();
			selectProductDetails1.getCatalogList().forEach(c -> {
				catalogs1.add(c.getCatalogId());
			});

			Thread.sleep(5000);
			queryrebaterateBean.setMchId(userId);
			queryrebaterateBean.setShopId(AgentUserId);
			queryrebaterateBean.setCatalogIds(catalogs1);

			queryrebaterateCall.setData(queryrebaterateBean);
			queryrebaterateCall.callService();
			Logger.verifyEquals("200", queryrebaterateCall.getString("code"), "验证code");
			JsonArray asJsonArray = queryrebaterateCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogRebateRateList");
			asJsonArray.forEach(b->{
				JsonObject asJsonObject = b.getAsJsonObject();
				Logger.verifyEquals(asJsonObject.get("rebateRate").getAsDouble(), 2.0,"验证rebateRate");
				});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryRebateRate_002() {
		Logger.start(true, "三级代理查询分销小店商品佣金，成功");
		try {
			int userId = 500949763;
			int AgentUserId = 500949764;
			int AgentUserId1 = 500949765;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(AgentUserId,"",productId,userId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			Thread.sleep(5000);

			ProductDetailsBean initGetProductDetails1 = AddAgentProductCallService.initGetProductDetails(userId,
					AgentUserId1, agentProductId, true);
			ProductDetailsDto selectProductDetails1 = AddAgentProductCallService
					.GetProductDetailsCall(initGetProductDetails1);

			List<String> catalogs1 = new ArrayList<>();
			selectProductDetails1.getCatalogList().forEach(c -> {
				catalogs1.add(c.getCatalogId());
			});


			
			Thread.sleep(5000);
			queryrebaterateBean.setMchId(AgentUserId);
			queryrebaterateBean.setShopId(AgentUserId1);
			queryrebaterateBean.setCatalogIds(catalogs1);

			queryrebaterateCall.setData(queryrebaterateBean);
			queryrebaterateCall.callService();
			Logger.verifyEquals("200", queryrebaterateCall.getString("code"), "验证code");
			JsonArray asJsonArray = queryrebaterateCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogRebateRateList");
			asJsonArray.forEach(b->{
				JsonObject asJsonObject = b.getAsJsonObject();
				Logger.verifyEquals(asJsonObject.get("rebateRate").getAsDouble(), 30.0,"验证rebateRate");
				});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}